/**
 * Created by AllanXu on 7/25/2016.
 */
define(function (require) {
	var Vue = require('vue'),
        Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities');
        
    var id = utilities.getParameterByName('id');
    // 认证
    var AuthDialog = require('auth_dialog');
    var authDialog = new AuthDialog();
    // 禁用申诉
    var Forbidden = require('forbidden');
    var forbidden = new Forbidden();
    
	var app = new Vue({
		el: '#tieba',
		data: {
			pageCount: '',
			pageno: 1,
            renderObj: {
                listArr: []
            },
            isShow: false,
            content: '',
            title: ''
		},
		methods:{
			init: function () {

                console.log(JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo))));

				_(Config.SERVICE.TIEBA.THEMELIST, {
                    "id": id,
                    "pageno": app.pageno,
                    "pagesize": 10
                }, function(data) {
                    if(app.pageno == 1){
                        app.renderObj.listArr = data.pageitems;
                        app.pageCount = data.pagecount;
                    }else{
                        app.renderObj.listArr = app.renderObj.listArr.concat(data.pageitems);
                    }
                    console.log(11);
                });
			},
            goDetail: function (id) {
                location.href = 'tieba_detail.html?id='+id;
            },
            switchDialog: function(){
                // 是否认证/禁用
                // base64TojsonObj
                var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                console.log(userInfo,23333)
                if(userInfo.member.sfrz==0){    // 认证
                    console.log("wtf")
                    authDialog.show();
                }else if(userInfo.member.sfjy==1){  // 禁用
                    console.log("wtf11")
                    forbidden.show();
                }else{
                    console.log("已认证、没有禁用");
                    _(Config.SERVICE.MATES.VERIFY+'?id=' + userId, {}, function(data) {
                        console.log(data,2444444);
                        if(data.errmsg.indexOf("择偶") > 0 ){
                            // 择偶条件没有完善跳转到择偶条件
                             if(confirm("为更好地为您提供服务，请在“个人编辑”中完善个人资料或择偶条件。")){
                                location.href = 'uc_mate_condition.html';
                             }
                        }else if(data.errmsg.indexOf("基本信息") > 0){
                            if(confirm("为更好地为您提供服务，请在“个人编辑”中完善个人资料或择偶条件。")){
                                location.href = 'uc_profile.html';
                             }
                        }else{
                            app.isShow = !app.isShow;
                        }
                    });

                }
            },
            goRelease: function () {
                if(app.title==''||app.content==''){
                    return alert("标题或内容没有输入！");
                };
                _(Config.SERVICE.TIEBA.RELEASE, {
                    "id": sessionStorage.id,
                    "themePost.themeId": id,  // 主题id
                    "themePost.title": app.title,
                    "themePost.content": app.content
                }, function(data) {
                    // console.log(data);
                    if(data.code==200){
                        alert("发布成功~");
                        app.isShow = false;
                        app.init();
                    }else{
                        alert("发布失败~");
                    }
                });
            }
		}
	});
	app.init();

	window.onscroll = function(){
        var scrolltop = document.body.scrollTop | document.documentElement.scrollTop;
        var clientHeight = document.documentElement.clientHeight;
        if(scrolltop + clientHeight  == document.body.clientHeight){
            if (app.pageno <= app.pageCount){
                app.pageno++;
                app.init();
            }else{
                app.bottomTxt = '到底儿了~';
            }
        }
    }
});